package com.piece.flowable.controller;

import com.piece.admin.api.dto.SysDictDTO;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.object.ObjectUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.module.flowable.model.WfCategory;
import com.piece.module.flowable.service.WfCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(tags = {"流程分类管理"})
@RequestMapping("/api/workflow/category")
public class WfCategoryController extends FrameWorkController {

    @Resource
    private WfCategoryService wfCategoryService;

    @PostMapping("/list")
    @AclPermission(code = "flowable:category:view")
    @ApiOperation(value = "查询流程分类列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = wfCategoryService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "flowable:category:view")
    @ApiOperation(value = "查询流程分类分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = wfCategoryService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "flowable:category:view")
    @ApiOperation(value = "加载流程分类表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        String id = Convert.toStr(getRequest().getParameter("id"), null);
        WfCategory wfCategory = new WfCategory();
        if (StringUtil.isNotEmpty(id)) {
            wfCategory = wfCategoryService.findById(Convert.toLong(id));
        }
        return success(wfCategory);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "flowable:category:add,flowable:category:edit")
    @ApiOperation(value = "保存流程分类表单数据")
    @ApiImplicitParam(value = "流程分类数据", dataTypeClass = WfCategory.class, type = "body")
    public AjaxResponse save() {
        WfCategory wfCategory;
        try {
            String id = Convert.toStr(getRequest().getParameter("id"));
            if (null != id) {
                wfCategory = wfCategoryService.findById(id);
            } else {
                wfCategory = new WfCategory();
            }
            initDB(wfCategory);
            wfCategoryService.insertOrUpdate(wfCategory);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "flowable:category:delete")
    @ApiOperation(value = "删除流程分类数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        wfCategoryService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "flowable:category:delete")
    @ApiOperation(value = "删除流程分类数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            wfCategoryService.delete(Convert.toStrArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @GetMapping("/dictGroup")
    @ApiOperation(value = "加载流程分类下拉菜单")
    public List<SysDictDTO> getDataGroup() {
        List<WfCategory> categories = wfCategoryService.findAll();
        if (ObjectUtil.isNotEmpty(categories)) {
            return categories.stream().map(categorie -> {
                SysDictDTO dict = new SysDictDTO();
                dict.setDictLabel(categorie.getCategoryName());
                dict.setDictLabelEn(categorie.getCategoryName());
                dict.setDictValue(categorie.getCode());
                return dict;
            }).collect(Collectors.toList());
        }

        return new ArrayList<>();
    }
}
